﻿using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

    public float m_speed = 1;
    public Transform m_rocket;


    private Transform _transform = null;
    private float m_rocketRate = 0;
    private int m_life=3;

    // Use this for initialization
    void Start() {
        _transform = this.transform;
    }

    // Update is called once per frame
    void Update() {
        m_rocketRate -= Time.deltaTime;
        if(m_rocketRate<=0)
        {
            m_rocketRate = 0.1f;

            //按空格或鼠标左键发射子弹
            if (Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0))
            {
                Instantiate(m_rocket, _transform.position, _transform.rotation);
            }
        }

        //纵向移动距离
        float movev = 0;
        //水平移动距离
        float moveh = 0;
        //按上键
        if (Input.GetKey(KeyCode.UpArrow))
        {
            movev -= m_speed * Time.deltaTime;
        }
        //按下键
        if (Input.GetKey(KeyCode.DownArrow))
        {
            movev += m_speed * Time.deltaTime;
        }
        //按左键
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            moveh += m_speed * Time.deltaTime;
        }
        //按右键
        if (Input.GetKey(KeyCode.RightArrow))
        {
            movev -= m_speed * Time.deltaTime;
        }
        //移动
        _transform.Translate(new Vector3(moveh, 0, movev));
    }
    
    void OnTriggerEnter(Collider other)
    {
        if (other.tag.CompareTo("PlayerRocket") != 0)
        {
            m_life -= 1;
            if (m_life <= 0)
            {
                Destroy(this.gameObject);
            }
        }
    }
}
